package com.base.service.impl;

import java.util.List;

import com.base.service.FlowworkitemService;
import com.common.bean.Flowworkitem;
import com.common.util.AbstractManager;
import com.common.util.PagerModel;
import com.common.util.StringToken;

public class FlowworkitemServiceImpl extends AbstractManager implements
		FlowworkitemService {

	public void addFlowworkitem(Flowworkitem flowworkitem) {
		String hql = "select nvl(max(workflowno),0) from Flowworkitem";
		
		long count = 0l;
		count = (Long) this.getHibernateTemplate().find(hql).get(0);
		count += 1l;
		
		flowworkitem.setWorkflowno(count);
		this.getHibernateTemplate().save(flowworkitem);
	}

	@SuppressWarnings("unchecked")
	public List<Flowworkitem> findAllFlowworkitem() {
		String hql = "from Flowworkitem order by workflowno";
		return this.getHibernateTemplate().find(hql);
	}

	public Flowworkitem findFlowworkitem(long workflowno) {
		return (Flowworkitem) this.getHibernateTemplate().load(Flowworkitem.class, workflowno);
	}

	public PagerModel findFlowworkitemForPager() {
		String hql = "from Flowworkitem order by workflowno";
		return searchPaginated(hql);
	}


	public void updateFlowworkitem(Flowworkitem flowworkitem) {
		this.getHibernateTemplate().update(flowworkitem);
	}

	public void removeFlowworkitem(long workflowno) {
		Flowworkitem flowworkitem = null;
		flowworkitem = (Flowworkitem) this.getHibernateTemplate().load(Flowworkitem.class, workflowno);
		
		if(flowworkitem != null) {
			this.getHibernateTemplate().delete(flowworkitem);
		}
	}

	@SuppressWarnings("unchecked")
	public Flowworkitem finaFlowworkitem(String deptcode, long cardmaintypeno, long typeid/*字典编号*/) {
		String hql = "from Flowworkitem flow where flow.department.deptcode = ? and flow.cardmaintype.cardmaintypeno = ? and flow.datadictByFlag.typeid = ?";
		List<Flowworkitem> list = this.getHibernateTemplate().find(hql,new Object[]{deptcode,cardmaintypeno,typeid});
		if(list == null || list.size() == 0	) {
			throw new RuntimeException("hava found none instance.");
		}
		
		Flowworkitem flow = null;
		for(Flowworkitem flowworkitem : list) {
			if(StringToken.DICT_FLOWRELEASESTATUS_RELEASE.equals(flowworkitem.getDatadictByReleasestatusno().getName())){
				flow = flowworkitem; //获得已发布的模版编号
			}
		}
		return flow;
	}

}
